# %%
from conn import (
    ac_task_col,
    clip_annotated_record,
    check_frame_label_col,
)


# %%

clip_ids = []
with open("clip_ids.txt", "r") as fp:
    for line in fp:
        clip_ids.append(line.strip())
print(len(clip_ids))

# %%
# 对前 100 个进行操作
target_clip_ids = clip_ids
annotate_type = 31

# %%


def handle_one_clip(clip_id):
    print("\nclip_id" + clip_id)

    # 3. ac task 记录更新成 2 进行中，并设置优先级
    cur_task = ac_task_col.find_one(
        dict(clip_ids=clip_id, annotate_type=annotate_type),
        {"task_name": 1, "status": 1, "priority": 1},
    )

    if cur_task is None:
        return False

    print(cur_task["task_name"], cur_task["priority"])

    update_info = dict(priority=4)
    if cur_task["status"] == 3:
        update_info["status"] = 2
    ac_task_col.update_one(
        {"_id": cur_task["_id"]},
        {"$set": update_info},
    )

    print("task status", cur_task["status"])
    # 1. 删除之前的标注记录？
    cnt = check_frame_label_col.count_documents(
        dict(clip_id=clip_id, annotate_type=annotate_type)
    )
    print("origin check frame cnt ", cnt)
    if cnt != 0:
        check_frame_label_col.delete_many(
            dict(clip_id=clip_id, annotate_type=annotate_type)
        )

    # 2. clip accept_status 变成 10 进行中
    cur_clip = clip_annotated_record.find_one(
        dict(clip_id=clip_id, annotate_type=annotate_type), {"accept_status": 1}
    )
    print("clip status", cur_clip.get("accept_status", -1))
    if cur_clip.get("accept_status", -1) != 10:
        clip_annotated_record.update_one(
            {"_id": cur_clip["_id"]},
            {"$set": {"accept_status": 10}},
        )

    return True


cnt = 0
for clip_id in target_clip_ids:
    if handle_one_clip(clip_id):
        cnt += 1
print(cnt)
